<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

// 课程分类
class Chapter extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    protected $SubjectModel;

    public function __construct()
    {
        parent::__construct();

        $this->model = model('Subject.Chapter');
        $this->SubjectModel = model('Subject.Subject');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            // 过滤请求参数中的html标签和取消两边你空白
            $this->request->filter(['strip_tags', 'trim']);
            // 获取查询参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['subject'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['subject'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = [
                'rows' => $list,
                'total' => $total
            ];

            return json($result);
        }

        return $this->view->fetch();
    }

    public function add($subid = null)
    {
        // 判断课程是否存在
        $subject = $this->SubjectModel->where(['id' => $subid]);

        if(!$subject){
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->param('row/a');

            $data = [
                'subid'=> $subid,
                'title' => $params['title'],
                'url' => $params['url'],
            ];

            $result = $this->model->validate('common/Subject/Chapter')->save($data);

            if ($result) {
                $this->success('添加课程章节成功');
                exit;
            } else {
                $this->error($this->model->getError());
                exit;
            }
        }

        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {   
        $row = $this->model->where(['id' => $ids])->find();

        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->param('row/a');

            $data = [
                'id'=> $ids,
                'title' => $params['title'],
                'url' => $params['url'],
            ];

            $result = $this->model->validate('common/Subject/Chapter')->isUpdate(true)->save($data);

            if ($result) {
                // 如果是修改了视频，则删除原视频
                if($row['url'] !== $data['url']){
                    $path = '.' . $row['url'];
                    // 如果文件存在，则删除
                    is_file($path) && @unlink($path);
                }

                $this->success('修改课程章节成功');
                exit;
            } else {
                $this->error($this->model->getError());
                exit;
            }
        }

        $this->assign('row', $row);

        return $this->view->fetch();
    }

    public function del($ids = NULL)
    {
        $list = $this->model->select($ids);

        if (!$list) {
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->where('id', 'in', $ids)->delete();

        if ($result) {
            $this->success('删除成功');
            exit;
        } else {
            $this->error($this->model->getError());
            exit;
        }
    }
}
